package com.ac.scum.log.web.model.server.controller;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.result.Result;
import com.ac.scum.config.bean.ServerConfigRestart;
import com.ac.scum.log.web.model.server.service.ServerConfigRestartService;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("server_restart_config")
public class ServerConfigRestartController {
    private final ServerConfigRestartService serverConfigRestartService;

    @GetMapping("list")
    public Result<List<ServerConfigRestart>> list() throws BaseException {
        return Result.ok(serverConfigRestartService.list(Wrappers.<ServerConfigRestart>lambdaQuery()
                .eq(ServerConfigRestart::getServerId, CurrentUserUtil.getCurrentServerId())
                .orderByDesc(ServerConfigRestart::getRestartTime)));
    }

    @DeleteMapping("by{id}")
    public Result<Boolean> deleteById(@PathVariable Long id) {
        return Result.ok(serverConfigRestartService.removeById(id));
    }

    @PutMapping("update")
    public Result<Boolean> update(@RequestBody ServerConfigRestart serverConfigRestart) {
        return Result.ok(serverConfigRestartService.updateById(serverConfigRestart));
    }

    @PostMapping("add")
    public Result<Boolean> add(@RequestBody ServerConfigRestart serverConfigRestart) throws BaseException {
        serverConfigRestart.setServerId(CurrentUserUtil.getCurrentServerId());
        return Result.ok(serverConfigRestartService.save(serverConfigRestart));
    }
}
